//
//  NetWork.m
//
//  Created by liuzhanyu on 16.3.4.
//  Copyright (c) 2016年 刘占宇. All rights reserved.
//

#import "NetWork.h"
#import "AFNetworking.h"
#import "SVProgressHUD.h"
#import "AFHTTPRequestOperation.h"

#import "MBProgressHUD.h"
@implementation NetWork

static BOOL isFirst = NO;

 BOOL isFirSHOW = NO;


static BOOL canUseNetWork =NO;

+ (void)GET:(NSString *)url parmater:(NSDictionary *)dic Block:(void (^)(NSData *data))block
{
    // 如果要检测网络状态的变化, 必须要用检测管理器的单例startMoitoring
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    // 检测网络连接的单例,网络变化时的回调方法
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        // 当前网络未知
        if (status == -1) {
            [SVProgressHUD showErrorWithStatus:@"网络连接失败,请查看网络是否连接正常！"];
        }
        // 当前无连接
        if (status == 0) {
            [SVProgressHUD showErrorWithStatus:@"网络连接失败,请查看网络是否连接正常！"];
        }
        // 当前为3G网络
        if (status == 1) {
           AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager manager]initWithBaseURL:BASEURL];
            // 对https加密解析
            manager.securityPolicy.allowInvalidCertificates = YES;
            // 设置超时时间
            [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
             manager.requestSerializer.timeoutInterval = 10.f;
            [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
            [manager.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", nil]];
            
            [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];

            //[LZYProgressHUD showProgressAnimation];
            [manager GET:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
                // 获取的数据传回去
                NSDictionary * ary = (NSDictionary *)responseObject;
                NSLog(@"%@",[ary objectForKey:@"meg"]);
                NSLog(@"%@",[ary objectForKey:@"state"]);
                NSString *state = [NSString stringWithFormat:@"%@",[ary objectForKey:@"state"]];
                if ([state isEqualToString:@"-1"]) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"denglu" object:nil];
                }else{
                    NSDictionary * dicc = (NSDictionary *)responseObject;
                    if ([[dicc objectForKey:@"code"] integerValue] == -1) {
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"denglu" object:nil];
                    }else{
                        block(responseObject);
                    }
                }
                [SVProgressHUD dismiss];
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"error = %@", error);
            }];
        }
        // 当前为WiFi
        if (status == 2) {
           AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager manager]initWithBaseURL:BASEURL];
            // 对https加密解析
            manager.securityPolicy.allowInvalidCertificates = YES;
            // 设置超时时间
            [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
            manager.requestSerializer.timeoutInterval = 10.f;
            [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
            
            [manager.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", nil]];
           
            [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];

            [manager GET:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
                if ([responseObject isKindOfClass:[NSArray class]]) {
                    block(responseObject);
                }else{
                    NSDictionary * dicc = (NSDictionary *)responseObject;
                    if ([[dicc objectForKey:@"code"] integerValue] == -1) {
                        [Tool ShowpromptWithMsg:@"网络出错"];
                    }else{
                        block(responseObject);
                    }
                }
                [SVProgressHUD dismiss];

            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                [SVProgressHUD dismiss];
                [Tool ShowpromptWithMsg:@"网络连接不稳定"];
                NSLog(@"error = %@", error);
            }];
        }
    }];
}

+ (void)getWithURL:(NSString *)url params:(NSDictionary *)params success:(void(^)(id responseObject))success failure:(void(^)(NSError *error))failure
{
    // AFNetWorking
    // 创建请求管理对象
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", nil]];
    // 发生请求
    [manager GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
    
}


+ (void)POST:(NSString *)url parmater:(NSDictionary *)dic Block:(void (^)(NSData *data))block
{
    // 如果要检测网络状态的变化, 必须要用检测管理器的单例startMoitoring
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    // 检测网络连接的单例,网络变化时的回调方法
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        // 当前网络未知
        if (status == -1) {
            static int flag = 1;
            if (flag == 1) {
                [Tool ShowpromptWithMsg:@"网络连接失败,请查看网络是否连接正常!"];
            }
            flag++;
        }
        // 当前无连接
        if (status == 0) {
            static int flag = 1;
            if (flag == 1) {
                [Tool ShowpromptWithMsg:@"网络连接失败,请查看网络是否连接正常!"];
            };
        }
        // 当前为3G网络
        if (status == 1) {
            AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager manager]initWithBaseURL:BASEURL];
            // 对https加密解析
            manager.securityPolicy.allowInvalidCertificates = YES;
            manager.requestSerializer = [AFHTTPRequestSerializer serializer];
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            [manager.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", nil]];
            [manager POST:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
                // 获取的数据传回去
                block(responseObject);
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"error = %@", error);
            }];
        }
        // 当前为WiFi
        if (status == 2) {
            AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager manager]initWithBaseURL:BASEURL];
            manager.requestSerializer = [AFHTTPRequestSerializer serializer];
            
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            // 对https加密解析
            manager.securityPolicy.allowInvalidCertificates = YES;
            [manager.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", nil]];
            [manager POST:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
                block(responseObject);
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"error = %@", error);
            }];
        }
    }];
}


//post上传
+(void)POST:(NSString *)strPath parameters:(NSDictionary *)dic  finish:(void (^)(NSData *data,NSError *error) )cb
{
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeNone];
    //调用这个函数的时候只检查一次网络。
    if (isFirst==NO) {
        //网络只有在startMonitoring 完成后才可以检查网络状态
        [[AFNetworkReachabilityManager sharedManager]startMonitoring]; //监听网络状态
        
        [[AFNetworkReachabilityManager sharedManager]setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            NSLog(@"status is %d",(int)status);
            canUseNetWork=YES;
        }];
        isFirst=YES;
    }
    BOOL isOK=[[AFNetworkReachabilityManager sharedManager]isReachable];
    //网络不可用时
    if (isOK==FALSE  && canUseNetWork ==YES ) {
        NSError *error=[NSError errorWithDomain:@"网络错误" code:100 userInfo:nil];
        cb (nil, error);
        return;
    }
    
    AFHTTPSessionManager *manager=[[AFHTTPSessionManager alloc]initWithBaseURL:BASEURL];
    
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    [manager POST:strPath parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        [SVProgressHUD dismiss];
        cb (responseObject,nil);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD dismiss];
        cb (nil,error);
    }];
    
    //用来计算上传的进度。
    [manager setTaskDidSendBodyDataBlock:^(NSURLSession *session, NSURLSessionTask *task, int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
        
        //nytesSent 本次上传了多少字节
        //totalBytesSent 累计上传了多少字节
        //totalBytesExpectedToSend 文件有多大，应该上传多少
        NSLog(@"task %@ progree is %f",task,totalBytesSent*1.0/totalBytesExpectedToSend);
    }];
}

//data数据流形式上传图片，key为字段
+(void)typePicturePOST:(NSString *)strPath parameters:(NSDictionary *)dic withPicureData:(NSData *)pictureData  withKey:(NSString *)key finish:(void (^)(NSData *data,NSError *error) )cb
{
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
    //调用这个函数的时候只检查一次网络。
    if (isFirst==NO) {
        //网络只有在startMonitoring 完成后才可以检查网络状态
        [[AFNetworkReachabilityManager sharedManager]startMonitoring]; //监听网络状态
        
        [[AFNetworkReachabilityManager sharedManager]setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            NSLog(@"status is %d",(int)status);
            canUseNetWork=YES;
        }];
        isFirst=YES;
    }
    BOOL isOK=[[AFNetworkReachabilityManager sharedManager]isReachable];
    //网络不可用时
    if (isOK==FALSE  && canUseNetWork ==YES ) {
        NSError *error=[NSError errorWithDomain:@"网络错误" code:100 userInfo:nil];
        cb (nil, error);
        return;
    }
    
    
    AFHTTPSessionManager *manager=[[AFHTTPSessionManager alloc]initWithBaseURL:BASEURL];
    
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    [manager POST:strPath parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        [formData appendPartWithFileData:pictureData name:key fileName:@"FileData.png" mimeType:@"image/png"];
        //       [formData appendPartWithFormData:pictureData name:key];
        //        不知道文件类型时
        //        NSData   *fileData=[NSData dataWithContentsOfFile:filePath];  //二进制数据
        //        NSString *fileName=[filePath lastPathComponent];              //文件名
        //        NSString *mimeType=[self getMIMEType:filePath];               //文件类型
        //        if (!mimeType) {
        //            mimeType = @"application/octet-stream";                   //不知道文件类型
        //        }
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        [SVProgressHUD dismiss];
        cb (responseObject,nil);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD dismiss];
        cb (nil,error);
    }];
    
    //用来计算上传的进度。
    [manager setTaskDidSendBodyDataBlock:^(NSURLSession *session, NSURLSessionTask *task, int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
        
        //nytesSent 本次上传了多少字节
        //totalBytesSent 累计上传了多少字节
        //totalBytesExpectedToSend 文件有多大，应该上传多少
        NSLog(@"task %@ progree is %f",task,totalBytesSent*1.0/totalBytesExpectedToSend);
    }];
}

+(void)typevideoPOST:(NSString *)strPath parameters:(NSDictionary *)dic withVideoData:(NSData *)VideoData  withKey:(NSString *)key finish:(void (^)(NSData *data,NSError *error) )cb{
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeNone];
    //调用这个函数的时候只检查一次网络。
    if (isFirst==NO) {
        //网络只有在startMonitoring 完成后才可以检查网络状态
        [[AFNetworkReachabilityManager sharedManager]startMonitoring]; //监听网络状态
        [[AFNetworkReachabilityManager sharedManager]setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            NSLog(@"status is %d",(int)status);
            canUseNetWork=YES;
        }];
        isFirst=YES;
    }
    BOOL isOK=[[AFNetworkReachabilityManager sharedManager]isReachable];
    //网络不可用时
    if (isOK==FALSE  && canUseNetWork ==YES ) {
        NSError *error=[NSError errorWithDomain:@"网络错误" code:100 userInfo:nil];
        cb (nil, error);
        return;
    }
    AFHTTPSessionManager *manager=[[AFHTTPSessionManager alloc]initWithBaseURL:BASEURL];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    [manager POST:strPath parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:VideoData name:key fileName:@"video.mp4" mimeType:@"video/mp4"];
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        [SVProgressHUD dismiss];
        cb (responseObject,nil);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD dismiss];
        cb (nil,error);
    }];
    
    //用来计算上传的进度。
    [manager setTaskDidSendBodyDataBlock:^(NSURLSession *session, NSURLSessionTask *task, int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
        NSLog(@"task %@ progree is %f",task,totalBytesSent*1.0/totalBytesExpectedToSend);
    }];

}


//data数据流形式上传多张图片，key为字段
+(void)typearrPicturePOST:(NSString *)strPath parameters:(NSDictionary *)dic withPicureData:(NSArray *)pictureData  withKeyArray:(NSArray *)keyArray finish:(void (^)(NSData *data,NSError *error) )cb
{
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeNone];
    //调用这个函数的时候只检查一次网络。
    if (isFirst==NO) {
        //网络只有在startMonitoring 完成后才可以检查网络状态
        [[AFNetworkReachabilityManager sharedManager]startMonitoring]; //监听网络状态
        
        [[AFNetworkReachabilityManager sharedManager]setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            NSLog(@"status is %d",(int)status);
            canUseNetWork=YES;
        }];
        isFirst=YES;
    }
    BOOL isOK=[[AFNetworkReachabilityManager sharedManager]isReachable];
    //网络不可用时
    if (isOK==FALSE  && canUseNetWork ==YES ) {
        NSError *error=[NSError errorWithDomain:@"网络错误" code:100 userInfo:nil];
        cb (nil, error);
        return;
    }
    
    AFHTTPSessionManager *manager=[[AFHTTPSessionManager alloc]initWithBaseURL:BASEURL];
    
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    [manager POST:strPath parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        for(int i=0;i<pictureData.count;i++){
            [formData appendPartWithFileData:pictureData[i] name:keyArray[i] fileName:@"headPicture.png" mimeType:@"image/png/jpg"];
        }
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        [SVProgressHUD dismiss];
        cb (responseObject,nil);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD dismiss];
        cb (nil,error);
    }];
    
    //用来计算上传的进度。
    [manager setTaskDidSendBodyDataBlock:^(NSURLSession *session, NSURLSessionTask *task, int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
        
        //nytesSent 本次上传了多少字节
        //totalBytesSent 累计上传了多少字节
        //totalBytesExpectedToSend 文件有多大，应该上传多少
        NSLog(@"task %@ progree is %f",task,totalBytesSent*1.0/totalBytesExpectedToSend);
    }];
}


/*  @brief  下载文件
*
*  @param paramDic   附加post参数
*  @param requestURL 请求地址
*  @param savedPath  保存 在磁盘的位置
*  @param success    下载成功回调
*  @param failure    下载失败回调
*  @param progress   实时下载进度回调
*/
- (void)downloadFileWithOption:(NSDictionary *)paramDic
                 withInferface:(NSString*)requestURL
                     savedPath:(NSString*)savedPath
               downloadSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
               downloadFailure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))error
                      progress:(void (^)(float progress)) progress
{
    
    //沙盒路径    //NSString *savedPath = [NSHomeDirectory() stringByAppendingString:@"/Documents/xxx.zip"];
    AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
    NSMutableURLRequest *request =[serializer requestWithMethod:@"POST" URLString:requestURL parameters:paramDic error:nil];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
    [operation setOutputStream:[NSOutputStream outputStreamToFileAtPath:savedPath append:NO]];
    [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
        float p = (float)totalBytesRead / totalBytesExpectedToRead;
        progress(p);
        NSLog(@"download：%f", (float)totalBytesRead / totalBytesExpectedToRead);
        
    }];
    
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(operation,responseObject);
        NSLog(@"下载成功");
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        success(operation,error);
        
        NSLog(@"下载失败");
        
    }];
    
    [operation start];
    
}
//get同步请求
//+ (void)requestsWithUrl:(NSString *)URLString params:(NSDictionary *)patams success:(void(^)(id result))block failue:(void(^)(NSError *error))failure{
//    AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
//    NSMutableURLRequest *request = [requestSerializer requestWithMethod:@"GET" URLString:URLString parameters:patams error:nil];
//    requestSerializer.timeoutInterval = 10;//超时处理
//    AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
//    AFHTTPResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializer];
//    [requestOperation setResponseSerializer:responseSerializer];
//    [requestOperation start];
//    [requestOperation waitUntilFinished];
//    [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//        block(responseObject);
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        failure(error);
//    }];
//    
//}
//+ (void)requestsWithUrl:(NSString *)URLString{
////第一步，创建URL
//
//NSURL *url = [NSURL URLWithString:URLString];
//
////第二步，通过URL创建网络请求
//
//NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
//    NSURLResponse * response = nil;
//    NSError * error = nil;
////第三步，连接服务器
//
//NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//  NSDictionary* dataDict = [NSJSONSerialization JSONObjectWithData:received options:0 error:nil];
//id str = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
//    
//    if (error) {
//        NSLog(@"error: %@",[error localizedDescription]);
//    }else{
//        NSLog(@"response : %@",response);
//        NSLog(@"%@",str);
//        NSLog(@"%@",dataDict);
//    }
//}
//data数据流形式上传图片，key为字段
+(void)typePictureYesPOST:(NSString *)strPath parameters:(NSDictionary *)dic withPicureData:(NSData *)pictureData  withKey:(NSString *)key finish:(void (^)(NSData *data,NSError *error) )cb
{
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
    //调用这个函数的时候只检查一次网络。
    if (isFirst==NO) {
        //网络只有在startMonitoring 完成后才可以检查网络状态
        [[AFNetworkReachabilityManager sharedManager]startMonitoring]; //监听网络状态
        
        [[AFNetworkReachabilityManager sharedManager]setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            NSLog(@"status is %d",(int)status);
            canUseNetWork=YES;
        }];
        isFirst=YES;
    }
    BOOL isOK=[[AFNetworkReachabilityManager sharedManager]isReachable];
    //网络不可用时
    if (isOK==FALSE  && canUseNetWork ==YES ) {
        NSError *error=[NSError errorWithDomain:@"网络错误" code:100 userInfo:nil];
        cb (nil, error);
        return;
    }
    
    
    AFHTTPSessionManager *manager=[[AFHTTPSessionManager alloc]initWithBaseURL:BASEURL];
    
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    [manager POST:strPath parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        [formData appendPartWithFileData:pictureData name:key fileName:@"FileData.png" mimeType:@"image/png"];
        //       [formData appendPartWithFormData:pictureData name:key];
        //        不知道文件类型时
        //        NSData   *fileData=[NSData dataWithContentsOfFile:filePath];  //二进制数据
        //        NSString *fileName=[filePath lastPathComponent];              //文件名
        //        NSString *mimeType=[self getMIMEType:filePath];               //文件类型
        //        if (!mimeType) {
        //            mimeType = @"application/octet-stream";                   //不知道文件类型
        //        }
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        [SVProgressHUD dismiss];
        cb (responseObject,nil);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD dismiss];
        cb (nil,error);
    }];
    
    //用来计算上传的进度。
    [manager setTaskDidSendBodyDataBlock:^(NSURLSession *session, NSURLSessionTask *task, int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
        
        //nytesSent 本次上传了多少字节
        //totalBytesSent 累计上传了多少字节
        //totalBytesExpectedToSend 文件有多大，应该上传多少
        NSLog(@"task %@ progree is %f",task,totalBytesSent*1.0/totalBytesExpectedToSend);
    }];
}

+ (void)GET:(NSString *)url parmater:(NSDictionary *)dic multi:(BOOL)multi Block:(void (^)(NSData *data))block
{
    // 如果要检测网络状态的变化, 必须要用检测管理器的单例startMoitoring
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    // 检测网络连接的单例,网络变化时的回调方法
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        // 当前网络未知
        if (status == -1) {
            [SVProgressHUD showErrorWithStatus:@"网络连接失败,请查看网络是否连接正常！"];
        }
        // 当前无连接
        if (status == 0) {
            [SVProgressHUD showErrorWithStatus:@"网络连接失败,请查看网络是否连接正常！"];
        }
        // 当前为3G网络
        if (status == 1) {
            AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager manager]initWithBaseURL:BASEURL];
            // 对https加密解析
            manager.securityPolicy.allowInvalidCertificates = YES;
            // 设置超时时间
            [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
            manager.requestSerializer.timeoutInterval = 10.f;
            [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
            [manager.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", nil]];
           
            
            if (!multi) {
                [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
            }
            
            //[LZYProgressHUD showProgressAnimation];
            [manager GET:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
                // 获取的数据传回去
                NSDictionary * ary = (NSDictionary *)responseObject;
                NSLog(@"%@",[ary objectForKey:@"meg"]);
                NSLog(@"%@",[ary objectForKey:@"state"]);
                NSString *state = [NSString stringWithFormat:@"%@",[ary objectForKey:@"state"]];
                if ([state isEqualToString:@"-1"]) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"denglu" object:nil];
                }else{
                    NSDictionary * dicc = (NSDictionary *)responseObject;
                    if ([[dicc objectForKey:@"code"] integerValue] == -1) {
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"denglu" object:nil];
                    }else{
                        block(responseObject);
                    }
                }
                [SVProgressHUD dismiss];
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"error = %@", error);
            }];
        }
        // 当前为WiFi
        if (status == 2) {
            AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager manager]initWithBaseURL:BASEURL];
            // 对https加密解析
            manager.securityPolicy.allowInvalidCertificates = YES;
            // 设置超时时间
            [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
            manager.requestSerializer.timeoutInterval = 10.f;
            [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
            
            [manager.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", nil]];
            
            if (!multi) {
                [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
            }
            
            [manager GET:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
                if ([responseObject isKindOfClass:[NSArray class]]) {
                    block(responseObject);
                }else{
                    NSDictionary * dicc = (NSDictionary *)responseObject;
                    if ([[dicc objectForKey:@"code"] integerValue] == -1) {
                        [Tool ShowpromptWithMsg:@"网络出错"];
                    }else{
                        block(responseObject);
                    }
                }
                [SVProgressHUD dismiss];
                
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                [SVProgressHUD dismiss];
                [Tool ShowpromptWithMsg:@"网络连接不稳定"];
                NSLog(@"error = %@", error);
            }];
        }
    }];
}

@end


